package com.jswl.training.springjpa.util;

public class HexUtils {
    //region Fields

    /**
     * 保存所有小写16进制字符
     */
    private static final char[] LOWER_CASE_HEXES = {
            '0', '1', '2', '3',
            '4', '5', '6', '7',
            '8', '9', 'a', 'b',
            'c', 'd', 'e', 'f'
    };

    /**
     * 保存所有大写16进制字符
     */
    private static final char[] UPPER_CASE_HEXES = {
            '0', '1', '2', '3',
            '4', '5', '6', '7',
            '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F'
    };

    //endregion

    //region Public Methods

    //region Bytes To String

    /**
     * 将字节数组转换成16进制小写字符串
     *
     * @param bytes 输入的字节数组
     * @return 16进制小写字符串
     */
    public static String bytesToLowerHex(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        } else {
            StringBuilder hex = new StringBuilder();
            for (byte b : bytes) {
                hex.append(byteToLowerHex(b));
            }
            return hex.toString();
        }
    }

    /**
     * 将字节数组转换成16进制小写字符串（字节中间有空格）
     *
     * @param bytes 输入的字节数组
     * @return 16进制小写字符串
     */
    public static String bytesToLowerHexWithSpace(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        } else {
            StringBuilder hex = new StringBuilder();
            for (byte b : bytes) {
                hex.append(byteToLowerHex(b));
                hex.append(' ');
            }
            return hex.substring(0, hex.length());
        }
    }

    /**
     * 将一个字节转换为十六进制小写字符串
     *
     * @param byteValue 输入的字节值
     * @return 16进制小写字符串
     */
    public static String byteToLowerHex(byte byteValue) {
        return new String(new char[]
                {
                        LOWER_CASE_HEXES[(byteValue >> 4) & 0x0F],
                        LOWER_CASE_HEXES[byteValue & 0x0F]
                });
    }

    /**
     * 将字节数组转换成16进制大写字符串
     *
     * @param bytes 输入的字节数组
     * @return 16进制大写字符串
     */
    public static String bytesToUpperHex(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        } else {
            StringBuilder hex = new StringBuilder();
            for (byte b : bytes) {
                hex.append(byteToUpperHex(b));
            }
            return hex.toString();
        }
    }

    /**
     * 将字节数组转换成16进制大写字符串（字节中间有空格）
     *
     * @param bytes 输入的字节数组
     * @return 16进制大写字符串
     */
    public static String bytesToUpperHexWithSpace(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        } else {
            StringBuilder hex = new StringBuilder();
            for (byte b : bytes) {
                hex.append(byteToUpperHex(b));
                hex.append(' ');
            }
            return hex.substring(0, hex.length());
        }
    }

    /**
     * 将一个字节转换为十六进制大写字符串
     *
     * @param byteValue 输入的字节值
     * @return 16进制大写字符串
     */
    public static String byteToUpperHex(byte byteValue) {
        return new String(new char[]
                {
                        UPPER_CASE_HEXES[(byteValue >> 4) & 0x0F],
                        UPPER_CASE_HEXES[byteValue & 0x0F]
                });
    }

    //endregion

    //region String To Bytes

    /**
     * 将16进制字符串转换为对应的字节数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexToBytes(String hex) throws NumberFormatException {
        if (hex == null || hex.length() == 0) {
            return null;
        } else {
            char[] hexChars = hex.toLowerCase().toCharArray();
            byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个

            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt(
                        String.format("%s%s", hexChars[i * 2], hexChars[i * 2 + 1]),
                        16);
            }

            return bytes;
        }
    }

    /**
     * 将用空格分隔的16进制字符串转换为对应的字节数组
     *
     * @param hex
     * @return
     */
    public static byte[] hexWithSpaceToBytes(String hex) throws NumberFormatException {
        if (hex == null || hex.length() == 0) {
            return null;
        } else {
            hex = hex.replace(" ", "");
            char[] hexChars = hex.toLowerCase().toCharArray();
            byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个

            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt(
                        String.format("%s%s", hexChars[i * 2], hexChars[i * 2 + 1]),
                        16);
            }

            return bytes;
        }
    }

    //endregion

    //endregion
}
